import { getToken } from '@/utils/auth'

// 登录页面
const loginPage = "/subpkg/consumer/login/login"
  
// 页面白名单
const whiteList = [
  loginPage, '/pages/index/index','/pages/myOrder/myOrder','/pages/my/my','/pages/shopInfo/shopInfo'
]

// 检查地址白名单
function checkWhite(url) {
  const path = url.split('?')[0]
  return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
  uni.addInterceptor(item, {
    invoke(to) {
		const {identityType} = uni.getStorageSync('userinfo')
		if(!identityType || identityType === 0 ) {
			if (checkWhite(to.url)) {
			  return true
			}
			uni.navigateTo({ url: loginPage })
			return false
		}
    },
    fail(err) {
      console.log(err)
    }
  })
})
